<?php

namespace backend\controllers;

use Yii;
use backend\models\CacheList;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backend\service\CacheListService;
use common\service\HttpService;
use yii\helpers\Url;

/**
 * 缓存控制器
 */
class CacheController extends BaseController {

    /**
     * @inheritdoc
     */
    public function behaviors() {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * 缓存列表
     * @return mixed
     */
    public function actionIndex() {
        $CacheListService = new CacheListService();
        $cacheList = $CacheListService->findCacheList();

        return $this->render('index', [
                    'cacheList' => $cacheList,
        ]);
    }

    /**
     * 添加缓存
     * @return mixed
     */
    public function actionCreate() {
        $model = new CacheList();

        if ($model->load(Yii::$app->request->post())) {
            $result = $model->save();
            if ($result) {
                 return $this->redirect(["tip/index", "msg" => '缓存添加成功', "url" => Url::toRoute(["/cache/index"])]);
            } else {
                 return $this->redirect(["tip/index", "msg" => '缓存添加失败', "url" => Url::toRoute(["/cache/index"])]);
            }
        } else {
            return $this->render('create', [
                        'model' => $model,
            ]);
        }
    }

    /**
     * 更新缓存
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id) {
        $model = $this->findModel($id);
        if(empty($model->id)){
           return $this->redirect(["tip/index", "msg" => '要更新的数据不存在，请勿乱操作。', "url" => Url::toRoute(["/cache/index"])]);
        }
        if ($model->load(Yii::$app->request->post())) {
            $result = $model->save();
            if ($result) {
                 return $this->redirect(["tip/index", "msg" => '缓存更新成功', "url" => Url::toRoute(["/cache/index"])]);
            } else {
                 return $this->redirect(["tip/index", "msg" => '缓存更新失败', "url" => Url::toRoute(["/cache/index"])]);
            }
            
        } else {
            return $this->render('update', [
                        'model' => $model,
            ]);
        }
    }

    /**
     * 删除缓存
     * @param string $id
     * @return mixed
     */
    public function actionDelCache($id) {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * 根据ID查找数据
     * @param string $id
     * @return CacheList the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = CacheList::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    /**
     * 根据ID,清除指定缓存。
     */
    public function actionQingCache($unique_mark) {
        $data = Yii::$app->cache->exists($unique_mark);
        if (!$data) {
            return $this->redirect(["tip/index", "msg" => '暂时没有缓存', "url" => Url::toRoute(["/cache/index"])]);
        }
        $del_rs = Yii::$app->cache->delete($unique_mark);
        if ($del_rs) {
            return $this->redirect(["tip/index", "msg" => '清除缓存成功', "url" => Url::toRoute(["/cache/index"])]);
        } else {
            return $this->redirect(["tip/index", "msg" => '清除缓存失败', "url" => Url::toRoute(["/cache/index"])]);
        }
    }

    /**
     * 缓存详情
     */
    public function actionCacheDetail ()
    {
        $cache_value = Yii::$app->request->get('cache_value');
        $data = Yii::$app->cache->get($cache_value);
        return $this->renderPartial('detail',["data"=>$data]);
    }

}
